<template>
  <div class="home-container">
    <!-- <el-button type="primary" @click="loginout">退出</el-button>
     -->
    <el-container>
      <!-- 头部区域 -->
      <el-header>
        <div>
          <img src="../../assets/logo.png" alt="" />
          <span>电商后台管理系统</span>
        </div>
        <el-button type="primary" @click="loginout" class="exitBtn"
          >退出</el-button
        >
      </el-header>
      <el-container>
        <!-- 侧边栏区域 -->
        <el-aside :width="isCollapse ? '3%' : '15%'">
          <div class="toggle" @click="toggleCollapse">|||</div>
          <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#ffd04b"
            default-active="activePath"
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false"
            router
          >
            <!-- 一级导航栏 -->
            <el-submenu
              :index="'/' + item.path"
              v-for="item in menusList"
              :key="item.id"
            >
              <template slot="title">
                <i :class="iconsObj[item.id]"></i>
                <span class="nav">{{ item.authName }}</span>
              </template>
              <!-- 二级导航栏 -->
              <el-menu-item
                v-for="menusChild in item.children"
                :key="menusChild.id"
                :index="'/' + menusChild.path"
                @click="saveNavState(menusChild.path)"
              >
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{ menusChild.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 主页内容区域 -->
        <el-main>
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      menusList: [],
      iconsObj: {
        '125': 'iconfont icon-yonghuguanli',
        '103': 'iconfont icon-permission',
        '101': 'iconfont icon-shangpinguanli',
        '102': 'iconfont icon-icon-dingdan',
        '145': 'iconfont icon-tongji'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  created() {
    this.getMenusList()
  },
  methods: {
    loginout() {
      // 清楚token缓存
      window.sessionStorage.clear()
      // 跳转到登录页面
      this.$router.push('/login')
    },
    // 获得侧边栏的数据
    async getMenusList() {
      // 解析结构, 并重命名为res
      const { data: res } = await this.$http.get('/menus')
      // console.log(await this.$http.get('/menus'))
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.menusList = res.data
    },
    // 折叠侧边栏
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 解决点击子菜单未出现高亮
    saveNavState(navState) {
      // 把点击的子菜单保存在sessionStorage中
      window.sessionStorage.setItem('navState', navState)
      this.activePath = window.sessionStorage.getItem('navState')
      console.log(this.activePath)
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  width: 100%;
  height: 100%;
  .el-container {
    height: 100%;
  }
}
.el-header {
  display: flex;
  position: relative;
  background-color: #373d41;
  color: #333;
  text-align: center;
  line-height: 60px;
  div {
    position: absolute;
    left: 0;
    color: #fff;
    font-size: 24px;
    img {
      width: 50px;
      height: 50px;
      border: 1px solid #eee;
      border-radius: 50%;
      vertical-align: middle;
      margin-right: 10px;
    }
  }
  .exitBtn {
    position: absolute;
    top: 50%;
    right: 0;
    transform: translate(-50%, -50%);
  }
}

.el-aside {
  background-color: #333744;
  color: #333;
  text-align: center;
  // line-height: 200px;
  .el-menu {
    border-right: 0;
    .el-submenu {
      text-align: left;
    }
  }
}
.iconfont {
  font-size: 20px;
  margin-right: 10px;
}
.nav {
  font-size: 20px;
}
.toggle {
  color: #fff;
  letter-spacing: 0.2em;
  margin-top: 10px;
  cursor: pointer;
}
.el-main {
  background-color: #eaedf1;
  color: #333;
}
</style>
